package com.fzdy.dataplatform.webutil;

import org.apache.poi.hssf.usermodel.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/10/20 0020.
 */
public class ObjListToExcel {
    public static File objListToExcel(String name, Map data) {
        Map<String, String> ziDuan = (Map<String, String>) data.get("ziDuan");
        List listData = (List) data.get("listData");
        Object[] keys = ziDuan.keySet().toArray();
        String[] ziDuanKeys = new String[keys.length];
        for (int k = 0; k < keys.length; k++) {
            String temp = keys[k].toString();
            int xuHao = Integer.parseInt(temp.substring(0, 1), 16);
            ziDuanKeys[xuHao] = temp.substring(1);
        }
        try {
            String path = name + ".xls";
            deleteFile(path);
            File newFile = new File(path);
            newFile.createNewFile();
            System.out.println("创建文件成功：" + path);
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet();

            for (int i = 0; i < listData.size()+1; i++) {
                HSSFRow row = sheet.createRow(i);
                Object obj =null;
                if (i != 0) {
                    obj = listData.get(i-1);
                }
                for (int j = 0; j < ziDuanKeys.length; j++) {
                    HSSFCell cell = row.createCell(j);
                    if (i == 0) {
                        sheet.setColumnWidth(j, 6000);
                        String sj=Integer.toHexString(j).toUpperCase();
                        String s=ziDuan.get(sj + ziDuanKeys[j]);
                        System.out.println(s);
                        cell.setCellValue(new HSSFRichTextString(s));
                    } else {
                        String ziDuanName = (String) ziDuanKeys[j];
                        ziDuanName = ziDuanName.replaceFirst(ziDuanName
                                .substring(0, 1), ziDuanName.substring(0, 1)
                                .toUpperCase());
                        ziDuanName = "get" + ziDuanName;
                        Class clazz = Class.forName(obj.getClass().getName());
                        Method[] methods = clazz.getMethods();
                        Pattern pattern = Pattern.compile(ziDuanName);
                        Matcher mat = null;
                        for (Method m : methods) {
                            mat = pattern.matcher(m.getName());
                            if (mat.find()) {
                                Object shuXing = m.invoke(obj, null);
                                if (shuXing != null) {
                                    cell.setCellValue(shuXing.toString());//这里可以做数据格式处理
                                } else {
                                    cell.setCellValue("");
                                }
                                break;
                            }
                        }
                    }
                }
            }
            OutputStream out = new FileOutputStream(path);
            wb.write(out);// 写入File
            out.flush();
            out.close();
            return newFile;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                System.out.println("删除单个文件" + fileName + "成功！");
                return true;
            } else {
                System.out.println("删除单个文件" + fileName + "失败！");
                return false;
            }
        } else {
            System.out.println("删除单个文件失败：" + fileName + "不存在！");
            return false;
        }
    }
}
